package day20171008;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by 14394 on 2017/10/8.
 */
public class WebSpider {
    public static void main(String[] args) throws IOException {

        //抓取网页所需要的内容并进行筛选
        Document d = Jsoup.connect("http://www.cnsoftbei.com/bencandy.php?fid=42&id=1587").get();
        Elements elements = d.select("table[style=width:757px]").select("tr:has(strong)");
        elements.remove();
        //所需要的行信息
        Elements elements1 = d.select("table[style=width:757px]").select("tr");

        //将抓取的内容按格式化存入Excel表格中
        String[] title = {"序号", "比赛名称", "获奖作品名称", "获奖名次", "指导教师", "参赛学生", "队伍名称", "参赛学校", "获奖作品介绍"};
        String path = "D:\\dev\\Spider.xls";
        long start = System.currentTimeMillis();
        WritableWorkbook wwb;
        OutputStream outputStream = null;
        Label label;
        Elements elements2 = null;

        try {
            outputStream = new FileOutputStream(path);
            wwb = Workbook.createWorkbook(outputStream);
            WritableSheet ws = wwb.createSheet("第六届程序设计大赛", 0);
            //读入表格属性列名称
            for (int i = 0; i < title.length; i++) {
                label = new Label(i, 0, title[i]);
                ws.addCell(label);
            }
            //行数
            int s = elements1.size();
            //表格内容填充
            //填入第一个属性列
            for (int i = 0; i < s; i++) {
                label = new Label(0, i + 1, "" + (i + 1));
                ws.addCell(label);
            }

            //第二个属性列
            for (int i = 0; i < s; i++) {
                label = new Label(1, i + 1, "中国软件杯第六届大学生软件设计大赛");
                ws.addCell(label);
            }


            for (int i = 1; i <= s; i++) {
                if (i == 1) {
                    label = new Label(3, i, "特等奖");
                    ws.addCell(label);
                } else if (i > 1 && i <= 16) {
                    label = new Label(3, i, "一等奖");
                    ws.addCell(label);
                } else if (i > 16 && i <= 76) {
                    label = new Label(3, i, "二等奖");
                    ws.addCell(label);
                } else {
                    label = new Label(3, i, "三等奖");
                    ws.addCell(label);
                }
            }

            for (int i = 0; i < s; i++) {
                elements2 = elements1.select("td:eq(6)");
                label = new Label(4, i + 1, elements2.get(i).text());
                ws.addCell(label);
            }

            for (int i = 0; i < s; i++) {
                elements2 = elements1.select("td:eq(3)");
                Elements elements3 = elements1.select("td:eq(4)");
                Elements elements4 = elements1.select("td:eq(5)");
                label = new Label(5, i + 1, elements2.get(i).text() + "," + elements3.get(i).text() + "," + elements4.get(i).text());
                ws.addCell(label);
            }

            for (int i = 0; i < s; i++) {
                elements2 = elements1.select("td:eq(1)");
                label = new Label(6, i + 1, elements2.get(i).text());
                ws.addCell(label);
            }

            for (int i = 0; i < s; i++) {
                elements2 = elements1.select("td:eq(2)");
                label = new Label(7, i + 1, elements2.get(i).text());
                ws.addCell(label);
            }



           /*  for(int i=0;i<elements1.size();i++){
              //所需要的列信息
               Elements elements2=elements1.get(i).select("td");
                for (int j=0;j<elements2.size();j++){
                    label=new Label(j,i+1,elements2.get(j).text());
                    ws.addCell(label);
                }

                if (i==1){
                    label=new Label(7,i,"特等奖");
                    ws.addCell(label);
                }else if (i>1&&i<=16){
                    label=new Label(7,i,"一等奖");
                    ws.addCell(label);
                }else if (i>16&&i<=76){
                    label=new Label(7,i,"二等奖");
                    ws.addCell(label);
                }else {
                    label=new Label(7,i,"三等奖");
                    ws.addCell(label);
                }

            }  */
            wwb.write();
            wwb.close();
            long end = System.currentTimeMillis();
            System.out.println("共用时间：" + (end - start) / 1000);
        } catch (Exception e) {
            System.out.println("未知异常，，");
            e.printStackTrace();
        }

    }
}
